package com.jvm123.blog.service;

import com.jvm123.blog.client.SearchClient;
import com.jvm123.blog.entity.Post;
import com.jvm123.blog.entity.PostVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author yawn http://jvm123.com
 *     2019/12/1 9:30
 */
@Service
public class SearchService {

    @Autowired
    SearchClient searchClient;


    public List<Post> search(String title, String content, String wd) {
        List<PostVo> posts;
        if (StringUtils.isNotBlank(title) || StringUtils.isNotBlank(content)) {
            posts = searchClient.search(title, content);
        } else if (StringUtils.isNotBlank(wd)) {
            posts = searchClient.search(wd);
        } else {
            posts = new ArrayList<>();
        }
        return posts.stream().map(PostVo::convert)
                .collect(Collectors.toList());
    }

}
